//滑动进度条
function change()
{
    var value = document.getElementById('progress').value;
    myVideo.currentTime=value;
    $(".blue-progress").css("width",(value*100)/allTime+"%");
    console.log(value);
}

//自动补0
function PrefixInteger(num, n) {
    return (Array(n).join(0) + num).slice(-n);
}

var progressTimeId;


//视频进度操作
var startX=0;
$(".video").on("mouseover",function(e)
{
    e.preventDefault();
    if(controlFlag)
    {
        $(".video-control").show();
    }


}).on("mouseout",function()
{
    if((myVideo.paused||myVideo.ended)&&controlFlag)
    {
        $(".video-control").show();
    }
    else {
        $(".video-control").hide();
    }

});


//定时器 获取当前播放位置
var timeRecordId;
function timeRecord()
{
    if(timeRecordId)
    {
        clearInterval(timeRecordId)
    }
    timeRecordId=setInterval(function(){

        if(myVideo.ended)
        {
            document.getElementById('progress').value=0;
            $(".blue-progress").css("width","0%");
            $(".current-time").text("00"+":"+"00"+":"+"00");
            $("#stopPlay").hide();
            $("#startPlay").show();
            $(".video-control").show();
            $(".icon-shareIcon").show();
        }
        else {
            var currentTime=parseInt(myVideo.currentTime);
            //console.log(currentTime)
            document.getElementById('progress').value=currentTime;
            $(".blue-progress").css("width",(currentTime*100)/allTime+"%");
            var hour=PrefixInteger(parseInt(currentTime/3600),2);
            var minute=PrefixInteger(parseInt(currentTime%3600/60),2);
            var second=PrefixInteger(parseInt(currentTime%3600%60),2);

            $(".current-time").text(hour+":"+minute+":"+second);
        }



    },1000)
}
timeRecord();


//视频播放控件
$(".definition li").click(function()
{
    $(".definition ul").hide();
    if($(this).text()==$(".definition div").text())
    {
        return;
    }

    $(".definition div").text($(this).text());
    $(".definition li").removeClass("active");
    $(this).addClass("active");
    var prevTime=myVideo.currentTime;

    $("#startPlay").hide();
    $("#stopPlay").show();

    //标清
    if($(this).index()==0)
    {
        //$("#video source").prop("src","video/video.mp4");

        myVideo=document.getElementById("video1");
        $("video").hide();
        $("#video1").show();
        clearInterval(timeRecordId)

        document.getElementById('progress').value=prevTime;
        myVideo.currentTime=prevTime;
        $(".blue-progress").css("width",(prevTime*100)/allTime+"%");

        timeRecord();
        myVideo.play();


    }
    //高清
    if($(this).index()==1)
    {
        //$("#video source").prop("src","video/video1.mp4");
        myVideo=document.getElementById("video");
        $("video").hide();
        $("#video").show();
        clearInterval(timeRecordId)

        document.getElementById('progress').value=prevTime;
        myVideo.currentTime=prevTime;
        $(".blue-progress").css("width",(prevTime*100)/allTime+"%");
        timeRecord();
        myVideo.play();

    }
    //超清
    if($(this).index()==2)
    {
        //$("#video source").prop("src","video/movie.mp4");
        myVideo=document.getElementById("video2");
        $("video").hide();
        $("#video2").show();
        clearInterval(timeRecordId)
        //myVideo.load();
        document.getElementById('progress').value=prevTime;
        myVideo.currentTime=prevTime;
        $(".blue-progress").css("width",(prevTime*100)/allTime+"%");
        timeRecord();
        myVideo.play();


    }


});

$(".definition div").click(function()
{
    $(".definition ul").toggle();
});

//视频播放
var myVideo=document.getElementById("video");

var startTimeId;
var allTime=0;
var controlFlag=0;
$("#play").on("click",function(e)
{
    e.preventDefault();
    $(myVideo).show();
    controlFlag=1;
    myVideo.play();  //视频开始播放
    $(".video-control").show();
    $("#stopPlay").show();
    $("#startPlay").hide();
    $("#play").hide();

    allTime = myVideo.duration;
    $("#progress").prop("max",allTime);
    console.log(allTime);
    var hour=PrefixInteger(parseInt(allTime/3600),2);
    var minute=PrefixInteger(parseInt(allTime%3600/60),2);
    var second=PrefixInteger(parseInt(allTime%3600%60),2);

    $(".all-time").text(hour+":"+minute+":"+":"+second);

    //调用广告tag显示函数
    setInterval(function(){videoAdTag(myVideo);},1000);

});

//暂停播放
$("#stopPlay").click(function(e)
{
    e.preventDefault();
    myVideo.pause();
    $("#stopPlay").hide();
    $("#startPlay").show();
    if(startTimeId)
    {
        clearTimeout(startTimeId);
    }
    if(progressTimeId)
    {
        clearTimeout(progressTimeId);
    }
    if(startTimeId)
    {
        clearTimeout(startTimeId);
    }
    $(".video-control").show();

});

//开始播放
var startTimeId;
$("#startPlay").click(function(e)
{
    e.preventDefault();
    myVideo.play();
    $("#stopPlay").show();
    $("#startPlay").hide();
    $("#play").hide();
    if(startTimeId)
    {
        clearTimeout(startTimeId);
    }
    if(progressTimeId)
    {
        clearTimeout(progressTimeId);
    }

});

$("#cancelFullscreen").on("touchstart",function(e)
{
    myVideo.pause();
    myVideo.webkitCancelFullScreen();
    $("#fullscreen").hide();
});



//加入愿望清单
$("#joinWishBtn").click(function()
{
    wish();
    //common.toast("成功加入");
});


var browser = {
    versions: function () {
        var u = navigator.userAgent, app = navigator.appVersion;
        return { //移动终端浏览器版本信息
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
            iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
            iPad: u.indexOf('iPad') > -1, //是否iPad
        };
    }(),
}


//分享浮层
$(".icon-shareIcon").click(function()
{
    if(isWeiXin())
    {
        $(".share-col").show();
        $(".weixin-share").show();
    }
    else {
        $(".share-col").show();
        $(".qr-code").hide();
        $(".share-list").show();
    }

});

$("#weixinShare").click(function()
{
    $(".share-col").show();
    $(".qr-code").show();
    $(".share-list").hide();
});

$("#friendsShare").click(function()
{
    $(".share-col").show();
    $(".qr-code").show();
    $(".share-list").hide();
});


$(".share-clode").click(function()
{

    $(".share-col").hide();

});
